package segmenter;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.recognition.impl.StopRecognition;
import org.ansj.splitWord.analysis.ToAnalysis;

public class ChineseSegmenterImpl implements ChineseSegmenter {

	/**
	 * this func will get chinese word from a list of stocks. You need analysis
	 * stocks' answer and get answer word. And implement this interface in the class
	 * : ChineseSegmenterImpl Example: 我今天特别开心 result : 我 今天 特别 开心
	 *
	 * @param stocks
	 *            stocks info
	 * @return chinese word
	 * @see ChineseSegmenterImpl
	 */
	@Override
	/**
	 * 原来是传StocksInfo[]进来,然后对其中的每一个answer进行处理,并且所有分词结果放在一个list中
	 * 现在改为传入一个String,返回List<term>
	 */
	public List<Term> getWordsFromInput(String s) {
		// TODO: write your code here

			// Result是每次循环单条ANSWER的的分词结果
			StopRecognition fitler = new StopRecognition();
			fitler.insertStopNatures("w"); // 过滤标点符号词性
			fitler.insertStopNatures("null");// 过滤null词性
			fitler.insertStopNatures("u");//过滤掉助词
			fitler.insertStopNatures("ude1");//过滤掉助词
			fitler.insertStopNatures("m");//过滤数字
			
			//对answer进行分词并加入到list中
			Result result = ToAnalysis.parse(s).recognition(fitler);//if load "stopwords.txt",replace fitler with setFilter
			return  result.getTerms();
	}
	  private StopRecognition setFilter() {
		  StopRecognition filter = new StopRecognition();
		  try {
			  FileInputStream fis = new FileInputStream("C:\\Users\\胡伟\\Desktop\\Java-WorkSpace\\java-example-2\\src\\main\\resources\\stopwords.txt");
			  InputStreamReader isr  = new InputStreamReader(fis,"UTF-8"); 
			  BufferedReader  reader = new BufferedReader(isr);
	 	        
	 	        String s = null;
	            while ((s = reader.readLine()) != null) {
	                filter.insertStopWords(s); //过滤词
	            }
	            reader.close();
		  }catch(IOException e) {
			  System.out.println(e.getMessage());
		  }
			filter.insertStopNatures("w"); // 过滤标点符号词性
			filter.insertStopNatures("null");// 过滤null词性
			filter.insertStopNatures("u");//过滤掉助词
			filter.insertStopNatures("ude1");//过滤掉助词
			filter.insertStopNatures("m");//过滤数字
		return filter;
	  }
}
